<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="open"
      :before-close="cancel"
      width="800px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        class="dialog-form"
      >
        <el-form-item label="开发板ID" prop="developId">
          <el-input
            v-model="form.developId"
            :disabled="addType == 'edit'"
            placeholder="请输入开发板ID"
          />
          <div class="tips">
            客户端在访问 OTA 请求时，需要设置格式 User-Agent 为 开发板ID/固件版本号
          </div>
        </el-form-item>

        <el-form-item label="产品类型" prop="productType">
          <el-select
            v-model="form.productType"
            placeholder="请选择产品类型"
            clearable
            size="small"
            filterable
          >
            <el-option
              :label="item"
              :value="item"
              v-for="(item, index) of products"
              :key="index"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>

        <el-form-item label="产品描述" prop="context">
          <el-input v-model="form.context" type="textarea" placeholder="请输入产品描述" />
        </el-form-item>

        <el-form-item label="序列号前缀" prop="prefix">
          <el-input v-model="form.prefix" placeholder="请输入序列号前缀" />
          <div class="tips">序列号包含厂家ID、产品ID，例如：TCLS_KEVINBOX2__</div>
        </el-form-item>

        <el-form-item label="授权算法" prop="algorithm">
          <el-input v-model="form.algorithm" disabled placeholder="请输入授权算法" />
          <!-- HMAC-SHA256 -->
        </el-form-item>

        <el-form-item label="授权类型" prop="authType">
          <el-select
            v-model="form.authType"
            placeholder="请选择授权类型"
            clearable
            size="small"
            disabled
          >
            <el-option label="开源版（开源硬件）" value="开源版（开源硬件）" />
          </el-select>
        </el-form-item>

        <el-form-item label="智能体模板" prop="templateId">
          <el-select
            v-model="form.templateId"
            placeholder="请选择智能体模板"
            clearable
            size="small"
            filterable
          >
            <el-option
              :label="item.nickName"
              :value="item.roleId"
              v-for="(item, index) of templates"
              :key="index"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { agentsList, addProductInfo, productById, updProductInfo } from "@/api/login";
export default {
  props: {
    open: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    addType: {
      type: String,
      default: "",
    },
    id: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      rules: {
        developId: [{ required: true, trigger: "blur", message: "开发板ID不能为空" }],
      },
      products: ["ESP32", "PC", "Android", "iOS", "Web", "Others"],
      templates: [],
      form: {},
      loading: false,
    };
  },
  watch: {
    open(newvalue, oldvalue) {
      if (newvalue && this.addType == "edit") {
        this.getproductById();
      }
    },
  },
  methods: {
    cancel() {
      this.$emit("cancel");
    },
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            const { form, addType } = this;
            this.loading = true;
            addType == "edit" ? await updProductInfo(form) : await addProductInfo(form);
            this.$modal.msgSuccess("操作成功");
            this.cancel();

            this.$emit("getList");
          } catch (error) {
            console.error("添加错误", error);
          } finally {
            this.loading = false;
          }
        }
      });
    },
    resetFormFun() {
      this.form = {
        developId: "",
        productType: "",
        productName: "",
        context: "",
        prefix: "",
        algorithm: "hmac-sha256",
        authType: "开源版（开源硬件）",
        templateId: null,
      };
      this.resetForm("form");
      console.log(this.form);
    },
    async getAgentsList() {
      try {
        const { data } = await agentsList("");
        this.templates = data || [];
      } catch (error) {
        console.error("获取智能体列表错误", error);
      }
    },
    async getproductById() {
      try {
        const { id } = this;
        const { data } = await productById(id);
        this.form = data || {};
      } catch (error) {
        console.error("获取智能体列表错误", error);
      }
    },
  },
  created() {
    this.getAgentsList();
  },
};
</script>

<style scoped lang="scss">
.dialog-form .el-select {
  width: 100%;
}
</style>
